﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Communergy.Common;
using CommunergyUploader;

namespace CurrentCostGUI
{
    public partial class SynchroniseMeters : Form
    {
        List<Communergy.Common.Meter> allServerMeters = new List<Communergy.Common.Meter>();
        List<Communergy.Common.Meter> allLocalMeters = new List<Communergy.Common.Meter>();
        IMeterRepository remoteMeterRepository;
        IMeterRepository localMetersRepository;

        public SynchroniseMeters()
        {
            InitializeComponent();
        }

        private void SynchroniseMeters_Load(object sender, EventArgs e)
        {
            try
            {
                remoteMeterRepository = CommunergyClientFactory.GetRemoteMeterStore();
                localMetersRepository = CommunergyClientFactory.GetLocalMeterStore();

                GetMeters();
            }
            catch
            {

            }

        }

        private void GetMeters()
        {
            var userID = GuiConfig.GetUserGuid();
            allServerMeters = remoteMeterRepository.GetMetersForUser(userID);
            allLocalMeters = localMetersRepository.GetMetersForUser(userID);
            BindData();          
        }

        private void BindData()
        {
            var serverGuids = from s in allServerMeters select s.MeterGuid;
            var localGuids = from s in allLocalMeters select s.MeterGuid;
            
            var localOnlyGuids =  localGuids.Except(serverGuids);
            var serverOnlyGuids = serverGuids.Except(localGuids);


            List<Communergy.Common.Meter> serverMeters;
            List<Communergy.Common.Meter> localMeters;

            if (chkHideLocallyRegisteredServerMeters.Checked)
            {
                serverMeters = allServerMeters.Where(s => serverOnlyGuids.Contains(s.MeterGuid)).ToList();
            }
            else
            {
                serverMeters = allServerMeters;
            }

            if (chkHideServerRegistered.Checked)
            {
                localMeters = allLocalMeters.Where(s => localOnlyGuids.Contains(s.MeterGuid)).ToList();
            }
            else
            {
                localMeters = allLocalMeters;
            }

            metersServer.DataSource = serverMeters.OrderBy(m => m.MeterName).ToList();
            metersLocal.DataSource = localMeters.OrderBy(m => m.MeterName).ToList();
        }

        private void chkHideLocallyRegisteredServerMeters_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void chkHideLocallyRegisteredServerMeters_Click(object sender, EventArgs e)
        {

        }

        private void chkHideServerRegistered_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
